package com.tyqx.hashchainbiz.controller.wx;

import com.tyqx.hashchainbean.module.WxCompany;
import com.tyqx.hashchainbiz.service.WxCompanyService;
import com.tyqx.hashchainbiz.service.WxSendFileService;
import hashchainutil.common.Dto;
import hashchainutil.common.DtoUtil;
import hashchainutil.common.EmptyUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @program: hashchain-spring
 * @description:
 * @author: Mr.Li
 * @create: 2020-10-30 15:03
 **/
@RestController
@RequestMapping("/wx/company")
@CrossOrigin
public class WxCompanyController {

    @Resource
    private WxCompanyService wxCompanyService;

    @Resource
    private WxSendFileService wxSendFileService;

    @RequestMapping(value = "/queryCompanyList", method = RequestMethod.POST)
    @ResponseBody
    public Dto queryCompanyList(){
        List<WxCompany> wxCompanyList = wxCompanyService.queryWxCompanyList();
        if(wxCompanyList.size()>0){
            return DtoUtil.returnSuccess("查询公司成功",wxCompanyList);
        }else{
            return DtoUtil.returnSuccess("未查询到公司记录");
        }
    }

    @RequestMapping(value = "/addWxCompany", method = RequestMethod.POST)
    @ResponseBody
    public Dto addWxCompany(@RequestBody WxCompany wxCompany) throws ParseException {
        String message = "";
        if(EmptyUtils.isNotEmpty(wxCompany)){
            Date currentDate = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date createTime = sdf.parse(sdf.format(currentDate));
            wxCompany.setCreateDate(createTime);
            int flag = wxCompanyService.insertWxCompany(wxCompany);
            if(flag>0){
                message = "添加公司成功";
            }else{
                message =  "添加公司失败";
            }
        }else{
            message = "信息为空，添加失败";
        }
        return DtoUtil.returnSuccess(message);
    }

    @RequestMapping(value = "/queryCompanyByOpenId", method = RequestMethod.POST)
    @ResponseBody
    public Dto queryCompanyByOpenId(String openId){
        List<WxCompany> wxCompanyList = wxCompanyService.queryWxCompanyByOpenId(openId);
        if(wxCompanyList.size()>0){
            return DtoUtil.returnSuccess("查询公司成功",wxCompanyList);
        }else{
            return DtoUtil.returnSuccess("未查询到公司记录");
        }
    }





}
